﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class bagScript : MonoBehaviour
{
  
    private CanvasGroup canvasGroup;
    [SerializeField]
    private GameObject slotPrefab;
    public int MyBagIndex { get; set; }
    private List<slotScript> slots = new List<slotScript>();

    public bool IsOpen { get { return canvasGroup.alpha > 0; } }

    public List<slotScript> MySlots { get => slots;  }
    public int MyEmptySlotCounter
    {
        get
        {
            int count=0;
            foreach (slotScript n in MySlots)
            {
                if(n.IsEmpty)
                {
                    count++;
                }
            }
            return count;
        }
    }

    public List<Item> GetItems()
    {
        List<Item> itms = new List<Item>();
        foreach(slotScript n in MySlots)
        {
            if (!n.IsEmpty)
            {
                
                foreach(Item itm in n.MyItems)
                {
                    itms.Add(itm);
                }
            }
        }
        return itms;
    }

    // Start is called before the first frame update
    void Awake()
    {
        canvasGroup = GetComponent<CanvasGroup>();
    }

    public void SlotAdd(int slotQuantity)
    {
        for(int i = 0; i < slotQuantity; i++)
        {
            slotScript slot=Instantiate(slotPrefab, transform).GetComponent<slotScript>();
            slot.MySlotIndex = i;
            slot.MyBag = this;
            MySlots.Add(slot);
        }
    }
    public void Toggle()
    {
        canvasGroup.alpha = canvasGroup.alpha > 0 ? 0 : 1;
        canvasGroup.blocksRaycasts = canvasGroup.blocksRaycasts ==true ? false : true;
    }
    public bool AddItem(Item item)
    {
        foreach(slotScript slot in MySlots)
        {
            if(slot.IsEmpty)
            {
                slot.AddItem(item);
                return true;
            }
        }
        return false;
    }
    public void Clear()
    {
        foreach(slotScript slot in slots)
        {
            slot.Clear();
        }
    }
    public void RemoveSlots(int slotQty)
    {
        foreach (slotScript slot in slots)
        {
            slot.MyBag = null;
            
        }
        slots.Clear();
      
    }
}
